package org.krishna.Dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.krishna.model.Employee;
import org.krishna.model.JobStatus;
import org.krishna.model.JobTitle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/*@Service("coreService")*/
@Service("saveData")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class SaveDataImpl implements SaveData{
	
	private static final Logger logger = LoggerFactory.getLogger(SaveDataImpl.class);
	
	@Autowired
    private SessionFactory sessionFactory;

	@Override
	public JobTitle getJobTitleById(int jobTitleId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(JobTitle.class);
		criteria.add(Restrictions.eq("jobId", jobTitleId));
		List<JobTitle> list = criteria.list();
		if (list != null && list.size() >= 1) {
			return (JobTitle) list.get(0);
		}else{
			return null;
		}	
	}

	@Override
	public JobStatus getJobStatusById(int jobStatusId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(JobStatus.class);
		criteria.add(Restrictions.eq("jobStatusId", jobStatusId));
		List<JobStatus> list = criteria.list();
		if (list != null && list.size() >= 1) {
			return (JobStatus) list.get(0);
		}else{
			return null;
		}	
	}

	@Override
	public boolean saveEmployeeDetails(Employee employee) {
		Boolean result = new Boolean(Boolean.FALSE);
		this.sessionFactory.getCurrentSession().save(employee);
		result = Boolean.TRUE;
		return result;
		
	}

	@Override
	public Employee getEmployeeById(String empId) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Employee.class);
		criteria.add(Restrictions.eq("employeeId", empId));
		List<Employee> list = criteria.list();
		if (list != null && list.size() >= 1) {
			return (Employee) list.get(0);
		}else{
			return null;
		}	
	}

	
	
	
	
	

}
